﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections;

namespace nl.tno.ict.multitouch.wpfmultitouch.controls
{
    public class FingerAdorner : Adorner
    {
        public TouchLibEvents.FingerObject Obj;

        #region constructor

        /// <summary>
        /// Be sure to call the base class constructor.
        /// </summary>
        /// <param name="adornedElement">Base element</param>
        public FingerAdorner(UIElement adornedElement, TouchLibEvents.FingerObject obj)
            : base(adornedElement)
        {
            this.Obj = obj;
            
            this.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(FingerAdorner_PreviewMouseLeftButtonDown);            
            this.PreviewMouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(FingerAdorner_PreviewMouseLeftButtonUp);
            this.PreviewMouseMove += new System.Windows.Input.MouseEventHandler(FingerAdorner_PreviewMouseMove);
            this.PreviewMouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(FingerAdorner_PreviewMouseRightButtonDown);
            this.PreviewMouseRightButtonUp += new System.Windows.Input.MouseButtonEventHandler(FingerAdorner_PreviewMouseRightButtonUp);
        }

        void FingerAdorner_PreviewMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is Window)
            {
                if (AdornedElement is MTCanvas)
                    ((MTCanvas)AdornedElement).MTCanvas_PreviewMouseRightButtonUp(sender, e);
                else if (AdornedElement is MTGrid)
                    ((MTGrid)AdornedElement).MTGrid_PreviewMouseLeftButtonUp(sender, e);
            }
            else
            {
                if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                    ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseRightButtonUp(sender, e);
                else if (((FrameworkElement)AdornedElement).Parent is MTGrid)
                    ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseLeftButtonUp(sender, e);
            }
        }

        void FingerAdorner_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is Window)
            {
                if (AdornedElement is MTCanvas)
                    ((MTCanvas)AdornedElement).MTCanvas_PreviewMouseRightButtonDown(sender, e);
                else if (AdornedElement is MTGrid)
                    ((MTGrid)AdornedElement).MTGrid_PreviewMouseLeftButtonDown(sender, e);
            }
            else
            {
                if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                    ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseRightButtonDown(sender, e);
                else if (((FrameworkElement)AdornedElement).Parent is MTGrid)
                    ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseLeftButtonDown(sender, e);
            }
        }

        void FingerAdorner_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is Window)
            {
                if (AdornedElement is MTCanvas)
                    ((MTCanvas)AdornedElement).MTCanvas_PreviewMouseMove(sender, e);
                else if (AdornedElement is MTGrid)
                    ((MTGrid)AdornedElement).MTGrid_PreviewMouseMove(sender, e);
            }
            else
            {
                if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                    ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseMove(sender, e);
                else if (((FrameworkElement)AdornedElement).Parent is MTGrid)
                    ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseMove(sender, e);
            }
        }

        void FingerAdorner_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is Window)
            {
                if (AdornedElement is MTCanvas)
                    ((MTCanvas)AdornedElement).MTCanvas_PreviewMouseLeftButtonDown(sender, e);
                else if (AdornedElement is MTGrid)
                    ((MTGrid)AdornedElement).MTGrid_PreviewMouseLeftButtonDown(sender, e);
            }
            else
            {
                if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                    ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseLeftButtonDown(sender, e);
                else if (((FrameworkElement)AdornedElement).Parent is MTGrid)
                    ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseLeftButtonDown(sender, e);
            }            
        }

        void FingerAdorner_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is Window)
            {
                if (AdornedElement is MTCanvas)
                    ((MTCanvas)AdornedElement).MTCanvas_PreviewMouseLeftButtonUp(sender, e);
                else if (AdornedElement is MTGrid)
                    ((MTGrid)AdornedElement).MTGrid_PreviewMouseLeftButtonUp(sender, e);
            }
            else
            {
                if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                    ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseLeftButtonUp(sender, e);
                else if (((FrameworkElement)AdornedElement).Parent is MTGrid)
                    ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseLeftButtonUp(sender, e);
            }
        }

        #endregion

        /// <summary>
        /// A common way to implement an adorner's rendering behavior is to override the OnRender
        /// method, which is called by the layout system as part of a rendering pass.
        /// </summary>
        /// <param name="drawingContext">The context to be drawn</param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect adornedElementRect = new Rect(new Size(((FrameworkElement)this.AdornedElement).ActualWidth, ((FrameworkElement)this.AdornedElement).ActualHeight));
            
            // Some arbitrary drawing implements.
            SolidColorBrush renderBrush = new SolidColorBrush(Colors.LightGreen);
            //renderBrush.Opacity = 0.4;
            Pen renderPen = new Pen(new SolidColorBrush(Colors.DarkGreen), 1.5);
            double renderRadius = 10.0;

            // Draw a circle at the position of the current finger.
            if (((FrameworkElement)AdornedElement).Parent is MTCanvas || ((FrameworkElement)AdornedElement).Parent is MTGrid)
                drawingContext.DrawEllipse(renderBrush, renderPen, new Point(this.Obj.CurrentPosition.X - MTCanvas.GetLeft(AdornedElement), this.Obj.CurrentPosition.Y - MTCanvas.GetTop((FrameworkElement)AdornedElement)), renderRadius, renderRadius);
            else
                drawingContext.DrawEllipse(renderBrush, renderPen, new Point(this.Obj.CurrentPosition.X, this.Obj.CurrentPosition.Y), renderRadius, renderRadius);            

        }        
    }

}
